package com.keur.money.data.dv.hepler

import android.Manifest
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.os.Build
import com.google.gson.Gson
import com.keur.money.app.MyApp
import com.keur.money.config.KeurConfigs
import com.keur.money.data.dv.utils.DvAplUtil
import com.keur.money.data.dv.utils.DvHdUtil
import com.keur.money.data.dv.utils.DvNetworkUtil
import com.keur.money.data.dv.utils.DvStoUtil
import com.keur.money.data.dv.utils.stringToZString
import com.keur.money.utils.helper.SharedHelper

/**
 * device
 */
class DvHelper() {
    val context = MyApp.app
    val product = KeurConfigs.KEUR_APPNAME
    val token = SharedHelper.getKeurSPString(SharedHelper.KEUR_SPKEY.KEUR_TOKEN)

    /**
     * AppList
     */
    fun gKeurAplData(): String? {
        return try {
            val array = arrayOf(Manifest.permission.INTERNET, Manifest.permission.ACCESS_NETWORK_STATE)
            val flags = PackageManager.GET_ACTIVITIES or PackageManager.GET_META_DATA
            val packages = context.packageManager.getPackagesHoldingPermissions(array, flags)
            val aplList = mutableListOf<HashMap<String, Any>>()
            for (packageInfo in packages) {
                val appInfo = packageInfo.applicationInfo
                appInfo?.let {
                    // 过滤掉系统应用
                    val isSys = (appInfo.flags and ApplicationInfo.FLAG_SYSTEM != 0)
                    if (!isSys) {
                        val appName = appInfo.loadLabel(context.packageManager).toString()
                        val appVersionCode = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
                            packageInfo.versionCode.toString()
                        } else {
                            packageInfo.longVersionCode.toString()
                        }

                        val jsonObject = HashMap<String, Any>().apply {
                            put("appType", 0)
                            put("appName", appName)
                            put("appVersionCode", appVersionCode)
                            put("dataPath", appInfo.dataDir ?: "")
                            put("sourcePath", appInfo.sourceDir ?: "")
                            put("appPackageName", packageInfo.packageName ?: "")
                            put("appVersionName", packageInfo.versionName ?: "")
                            put("installTime", packageInfo.firstInstallTime.toString())
                            put("lastTime", packageInfo.lastUpdateTime.toString())
                            put("flags", appInfo.flags.toString())
                        }
                        aplList.add(jsonObject)
                    }
                }
            }
            Gson().toJson(aplList).trim().stringToZString()
        } catch (e: Exception) {
            null
        }
    }

    /**
     * Device
     */
    fun gKeurDvData(): String? {
        return try {
            val data = hashMapOf<String, Any>().apply {
                put("token", token)
                put("product", product)
                put("imei", "")
                put("version", "30")
                put("app", product)
                put("manufacturer", Build.MANUFACTURER ?: "")
                put("brand", Build.BRAND ?: "")
                put("deviceName", Build.DEVICE ?: "")
                put("phoneModel", Build.PRODUCT ?: "")
                put("sysVersion", Build.VERSION.RELEASE ?: "")
                put("mac", DvNetworkUtil.mcads())
                put("laguage", DvAplUtil.lang())
                put("area", DvAplUtil.area())
                put("screenHeight", DvAplUtil.dHht(context))
                put("screenWidth", DvAplUtil.dwh(context))
                put("networkData", DvAplUtil.sioName(context))
                put("frontCameraPixels", DvHdUtil.gfsiz(context))
                put("rearCameraPixels", DvHdUtil.bgsiz(context))
                put("ram", DvStoUtil.talRSzi(context))
                put("rom", DvStoUtil.toaDis())
                put("ip", DvNetworkUtil.iads(context))
                put("ip2", DvNetworkUtil.iads(context))
                put("networkEnvironment", DvAplUtil.wiType(context))
                put("cpu", DvAplUtil.cpSerial())
            }
            Gson().toJson(data).trim()
        } catch (e: Exception) {
            null
        }
    }

    /**
     * hardware
     */
    fun gKeurHdData(): String? {
        return try {
            val data = hashMapOf<String, Any>().apply {
                put("operatingSystem", "ANDROID")
                put("model", Build.MODEL.toString())
                put("serial", "")
                put("productionDate", Build.TIME.toString())
                put("sdkVersion", Build.VERSION.SDK_INT.toString())
                put("phoneType", DvAplUtil.phType(context))
                put("versionCode", DvAplUtil.verCode(context))
                put("versionName", DvAplUtil.verName(context))
                put("androidId", DvAplUtil.anroiId(context))
                put("telephony", DvAplUtil.sioName(context))
                put("isVpn", DvNetworkUtil.vsta(context))
                put("isProxyPort", DvNetworkUtil.isvapro())
                put("isDebug", DvAplUtil.isDg(context))
                put("simState", DvAplUtil.siState(context))
                put("root", DvHdUtil.chrot())
                put("physicalSize", DvHdUtil.pysiz(context))
                put("displayLanguage", DvAplUtil.disLang())
                put("iso3Language", DvAplUtil.isoLang())
                put("iso3Country", DvAplUtil.isoCtry())
                put("networkOperatorName", DvAplUtil.sioName(context))
                put("networkType", DvAplUtil.wiType(context))
                put("timeZoneId", DvAplUtil.tzID())
                put("elapsedRealtime", DvAplUtil.elaptime())
                put("sensorList", DvHdUtil.sorList(context))
                put("lastBootTime", DvAplUtil.lastBTime())
                put("rootJailbreak", DvHdUtil.chrot())
                put("keyboard", DvHdUtil.kbard(context))
                put("isSimulator", DvHdUtil.isVirso())
                put("dbm", DvHdUtil.gmdb(context)) //
                put("phoneNumber", "")
            }
            Gson().toJson(data).trim().stringToZString()
        } catch (e: java.lang.Exception) {
            null
        }
    }

    /**
     * wifi
     */
    fun gKeurNetwork(): String? {
        return try {
            val data = hashMapOf<String, Any>().apply {
                put("currentWifi", DvNetworkUtil.cwfnow(context))
                put("configuredWifi", "")
                put("wifiCount", "")
            }
            Gson().toJson(data).trim().stringToZString()
        } catch (e: Exception) {
            null
        }
    }

    /**
     * storage
     */
    fun gKeurSto(): String? {
        return try {
            val data = hashMapOf<String, Any>().apply {
                put("imei", "")
                put("containSd", DvStoUtil.youKa())
                put("ramCanUse", DvStoUtil.remaiSiz(context))
                put("ramTotal", DvStoUtil.talRSzi(context))
                put("cashCanUse", DvStoUtil.disRema())
                put("cashTotal", DvStoUtil.toaDis())
                put("extraSD", DvStoUtil.isYoKa())
                put("internalTotal", DvStoUtil.talInerSiz(context))
                put("internalAvailable", DvStoUtil.avalInerSiz(context))
                put("ramDidUsed", DvStoUtil.useSiz(context))
                put("cashDidUsed", DvStoUtil.dissee())
                put("sdCardTotal", DvStoUtil.toaYoKa())
            }
            Gson().toJson(data).trim().stringToZString()

        } catch (e: Exception) {
            null
        }
    }
}